﻿function addCart(produtId) {
    $.post("/Cart/Add",
        {
            productId: produtId
        },
        function (data, status) {
            if (status == "success") {
                if (data.code == "0") {
                    alert("购物车添加成功");
                } else {
                    alert("code: " + data.code + "\nmsg: " + data.message);
                }
            } else {
                alert("Data: " + data + "\nStatus: " + status);
            }
        }
    );
}
function buy(produtId) {
    window.open("/Purchase/Buy?productId=" + produtId, "_blank");
}

function showGrid() {
    $("#grid").addClass("active");
    $("#gridTab").addClass("active");
    $("#list").removeClass("active");
    $("#listTab").removeClass("active");
}
function showList() {
    $("#list").addClass("active");
    $("#listTab").addClass("active");
    $("#grid").removeClass("active");
    $("#gridTab").removeClass("active");
}

function drag(e, t) {
    //console.log("正在拖动");
    //console.log(e);
    //console.log(e.offsetX)
    //console.log(t)
    //$(t).css("left", e.offsetX + "%");
}

function checkActive(t, tt)
{
    $(t).toggleClass("por-dsc-span-active");
    $(t).parent().children().not(t).removeClass("por-dsc-span-active");
    if (tt == "color") {
        $("#color").val($(t).text());
    } else {
        $("#size").val($(t).text());
    }
}

function tabProductImage(id,t) {
    var jqId = "#" + id;
    $(jqId).addClass("active");
    $(jqId).parent().children().not($(jqId)).removeClass("active");
    //$(t:img).addClass("por-dsc-span-active");
    //$(t).parent("ul").children().not(t).removeClass("por-dsc-span-active");
}

function CheckAddr(o) {
    $(o).addClass("checked");
    $(o).parent("label.next-radio-wrapper").addClass("checked");
    var p = $(o).parents(".addr-item-wrapper");
    //console.log(p);
    p.addClass("addr-selected");
    var others = p.siblings(".addr-item-wrapper");
    others.removeClass("addr-selected");
    others.find(".next-radio-wrapper").removeClass("checked");
    others.find(".next-radio").removeClass("checked");
    //修改确认地址
    var province = $(o).siblings(".next-radio-label").children(".provinceName").text();
    var city = $(o).siblings(".next-radio-label").children(".cityName").text();
    var district = $(o).siblings(".next-radio-label").children(".areaName").text();
    var street = $(o).siblings(".next-radio-label").children(".townName").text();
    var detail = $(o).siblings(".next-radio-label").children(".addressDetail").text();
    var phone = $(o).siblings(".next-radio-label").children(".mobile").text();
    var name = $(o).siblings(".next-radio-label").children(".reciver").text();
    $(".confirmAddr-addr .confirmAddr-addr-bd").text(province + city + district + street + detail);
    $(".confirmAddr-addr-user .confirmAddr-addr-bd").text(name + " " + phone);
    //console.log(name + " " + phone);
    //增加寄送至几个字
    $(o).parent("label.next-radio-wrapper").before('<div class="selected-description"> <i class="marker">&hearts;</i><span class="marker-tip">寄送至</span> </div>');
    others.find(".selected-description").remove();
    //
    $("#addr").val(province + city + district + street + detail + name + " " + phone);

}

function confirmTotalPrice(id) {
    var quantity = parseInt($("#quantity_" + id).val());
    var preferetital = parseFloat($("#preferentitalprice_" + id).val());
    console.log(quantity);
    console.log(preferetital);
    console.log(quantity * preferetital);
    $("#item-row__price-item_" + id).text(quantity * preferetital);
    var total_price = 0.0;
    $("span[name='item_row_price']").each(function () {
        total_price += parseFloat($(this).text());
    });
    $("#total_price1").text(total_price);
    $("#total_price2").text(total_price);
}

function addCartByForm() {
    $("#detailForm").attr("action","/Cart/AddWithForm");
    $("#detailForm").submit();
}

function addPurchaseByForm() {
    $("#detailForm").attr("action", "/Purchase/BuyWithForm");
    $("#detailForm").submit();
}

function checkCartProduct(t) {
    var subtotal = parseFloat($(t).parent("td").siblings(".product-subtotal").text());
    var carttotal = parseFloat($(".cart-total .cart-black span").text());
    if ($(t).prop("checked"))
    {
        //选中
        $(".cart-total .cart-black span").text((carttotal + subtotal).toFixed(2));
    } else {
        //未选中
        $(".cart-total .cart-black span").text((carttotal - subtotal).toFixed(2));
    }
}

function checkSubmit() {
    var carttotal = parseFloat($(".cart-total .cart-black span").text());
    if (carttotal < 0.01) {
        window.alert("没有选择任何商品。");
        return false;
    } else {
        return true;
    }
}